% ctex-luacjk-engine.def: for LuaTeX engine with luatexja
% vim:ft=tex

\RequirePackage{xpatch}
\RequirePackage{luatexja-fontspec}[2013/05/14]

\ExplSyntaxOn

\tl_if_exist:NF \CJKrmdefault { \tl_const:Nn \CJKrmdefault { rm } }
\tl_if_exist:NF \CJKsfdefault { \tl_const:Nn \CJKsfdefault { sf } }
\tl_if_exist:NF \CJKttdefault { \tl_const:Nn \CJKttdefault { tt } }

\xapptocmd \rmfamily { \CJKfamily { \CJKrmdefault } } { } { }
\xapptocmd \sffamily { \CJKfamily { \CJKsfdefault } } { } { }
\xapptocmd \ttfamily { \CJKfamily { \CJKttdefault } } { } { }

\tl_if_exist:NF \CJKfamilydefault
  { \tl_const:Nn \CJKfamilydefault { \CJKrmdefault } }
\tl_set:Nn \kanjifamilydefault { \CJKfamilydefault }

\DeclareKanjiFamily { \kanjiencodingdefault } { \kanjifamilydefault } { }
\DeclareFontShape
  { \kanjiencodingdefault } { \kanjifamilydefault }
  { \mddefault } { \kanjishapedefault }
  { <-> psft:SimSun:cid=Adobe-GB1-5;jfm=quanjiao } { }
\DeclareFontShape
  { \kanjiencodingdefault } { \kanjifamilydefault }
  { \bfdefault } { \kanjishapedefault }
  { <-> psft:SimHei:cid=Adobe-GB1-5;jfm=quanjiao } { }
\DeclareSymbolFont { mincho }
  { \kanjiencodingdefault } { \kanjifamilydefault }
  { \mddefault } { \kanjishapedefault }
\SetSymbolFont { mincho } { bold }
  { \kanjiencodingdefault } { \kanjifamilydefault }
  { \bfdefault } { \kanjishapedefault }

\AtBeginDocument
  {
    \prop_get:NxNT \g_ctex_ltj_family_name_prop { \CJKfamilydefault } \l_tmpa_tl
      {
        \tl_const:Nx \c_ctex_ltj_math_family_tl { \l_tmpa_tl }
        \DeclareSymbolFont { mincho }
          { \kanjiencodingdefault } { \c_ctex_ltj_math_family_tl }
          { \mddefault } { \kanjishapedefault }
        \cs_if_exist:cTF
          { \kanjiencodingdefault/\c_ctex_ltj_math_family_tl/\bfdefault/\kanjishapedefault }
          {
            \SetSymbolFont { mincho } { bold }
              { \kanjiencodingdefault } { \c_ctex_ltj_math_family_tl }
              { \bfdefault } { \kanjishapedefault }
          }
          {
            \SetSymbolFont { mincho } { bold }
              { \kanjiencodingdefault } { \c_ctex_ltj_math_family_tl }
              { \mddefault } { \kanjishapedefault }
          }
      }
  }
\cs_generate_variant:Nn \prop_get:NnNT { Nx }

\prop_new:N \g_ctex_ltj_family_name_prop
\cs_new_protected:Npn \ctex_ltj_set_family:nnn #1#2#3
  {
    \ltj_fontspec_select:nn {#2} {#3}
    \prop_gput:NnV \g_ctex_ltj_family_name_prop {#1} \l_fontspec_family_tl
  }

\RenewDocumentCommand \kanjifamily { m }
  {
    \tl_set:Nx \k@family {#1}
    \prop_get:NVNF \g_ctex_ltj_family_name_prop \k@family \k@family { }
  }

\NewDocumentCommand \CJKfamily { m } { \ctex_ltj_switch_family:x {#1} }
\cs_new_protected_nopar:Npn \ctex_ltj_switch_family:n #1
  {
    \prop_get:NnNTF \g_ctex_ltj_family_name_prop {#1} \k@family
      { \selectfont \tex_ignorespaces:D }
      {
        \prop_if_empty:NF \g_ctex_ltj_family_name_prop
          {
            \seq_if_in:NnF \g_ctex_ltj_unknown_family_seq {#1}
              {
                \seq_gput_right:Nn \g_ctex_ltj_unknown_family_seq {#1}
                \msg_warning:nnn { ctex } { CJKfamily-Unknown } {#1}
              }
          }
      }
  }
\cs_generate_variant:Nn \ctex_ltj_switch_family:n { x }
\seq_new:N \g_ctex_ltj_unknown_family_seq

\msg_new:nnn { ctex } { CJKfamily-Unknown }
  {
    Unknown~CJK~family~'#1'~is~ignored.\\
    Try~to~use~\token_to_str:N \setCJKfamilyfont{#1}[...]{...}\
    to~define~it.
  }
\cs_new_eq:NN \CJKfontspec            \jfontspec
\cs_new_eq:NN \newCJKfontface         \newjfontface
\cs_new_eq:NN \newCJKfontfamily       \newjfontfamily
\cs_new_eq:NN \addCJKfontfeatures     \addjfontfeatures
\cs_new_eq:NN \defaultCJKfontfeatures \defaultjfontfeatures

\NewDocumentCommand \setCJKfamilyfont { m O { } m }
  { \use:x { \ctex_ltj_set_family:nnn {#1} {#2} {#3} } }
\NewDocumentCommand \setCJKmainfont { O { } m }
  {
    \use:x { \ctex_ltj_set_family:nnn { \CJKrmdefault } {#1} {#2} }
    \normalfont
  }
\cs_new_eq:NN \setCJKromanfont \setCJKmainfont
\NewDocumentCommand \setCJKsansfont { O { } m }
  {
    \use:x { \ctex_ltj_set_family:nnn { \CJKsfdefault } {#1} {#2} }
    \normalfont
  }
\NewDocumentCommand \setCJKmonofont { O { } m }
  {
    \use:x { \ctex_ltj_set_family:nnn { \CJKttdefault } {#1} {#2} }
    \normalfont
  }

\@onlypreamble\setCJKmainfont
\@onlypreamble\setCJKsansfont
\@onlypreamble\setCJKmonofont
\@onlypreamble\setCJKromanfont
\@onlypreamble\defaultCJKfontfeatures

\tl_gset:cn { ver@CJK.sty } { 9999/99/99 }

\NewDocumentCommand \CJKglue { }
  { \skip_horizontal:n { \ltjgetparameter { kanjiskip } } }
\NewDocumentCommand \CJKspace { }
  { \ltjsetparameter { autospacing = false } }
\NewDocumentCommand \CJKnospace { }
  { \ltjsetparameter { autospacing = true } }

\ifCTEX@punct
  \defaultCJKfontfeatures { JFM = quanjiao }
\else
  \defaultCJKfontfeatures { JFM = mono }
\fi

\ExplSyntaxOff
\endinput
